
package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.Persona;
import edu.upc.clase.demo.service.PersonaService;
import edu.upc.clase.demo.service.UsuarioService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author dramos
 */
@Controller("personaController")
public class PersonaController {
    
    @Autowired
    private PersonaService personaService;
    
    @Autowired
    private UsuarioService usuarioService;
    
    private static Logger log = LoggerFactory.getLogger(PersonaController.class);

    @RequestMapping("/personas/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("personas/index");
        List<Persona> personas = personaService.buscarTodos();
        mav.addObject("personas", personas);
        return mav;
    }
    
    @RequestMapping(value = "/personas/new", method = RequestMethod.GET)
    public ModelAndView newPersona() {
        ModelAndView mav = new ModelAndView("personas/new");
        Persona persona = new Persona();
        mav.getModelMap().put("persona", persona);
        return mav;
    }    
    
    @RequestMapping(value = "/personas/new", method = RequestMethod.POST)
    public String createPersona(@ModelAttribute("persona")Persona persona, SessionStatus status) {    
        personaService.insertar(persona);
        
        status.setComplete();

        return "redirect:/pages/usuarios/index";
        
    }    
    
}
